package nju.software.xkxt.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.software.xkxt.biz.vo.UserVO;
import nju.software.xkxt.service.ScoreService;
import nju.software.xkxt.service.UserService;
import nju.software.xkxt.service.model.ScoreModel;
import nju.software.xkxt.service.model.UserModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ScoreController {

	ScoreService scoreService;
	UserService userService;

	@RequestMapping(value = "/showuser.do", method = RequestMethod.GET)
	public String showUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {

		String cId = request.getParameter("cid");
		String tId = request.getParameter("tid");
		List<UserModel> userModels = new ArrayList<UserModel>();
		List<ScoreModel> scoreModels = scoreService.getScoreModelsByCidAndTid(
				Integer.valueOf(cId), Integer.valueOf(tId));
		for (ScoreModel scoreModel : scoreModels) {
			UserModel userModel = userService.getUserById(scoreModel.getUId());
			userModels.add(userModel);
		}

		List<UserVO> userVOs = new ArrayList<UserVO>();
		for (UserModel src : userModels) {
			UserVO userVO = new UserVO(src);
			ScoreModel scoreModel = scoreService.getScoreByUidAndCid(
					src.getUserId(), Integer.parseInt(cId));
			Integer score = scoreModel.getScore();
			if (score != null) {
				userVO.setScore(scoreModel.getScore().toString());
			}
			userVOs.add(userVO);
		}
		map.addAttribute("UserList", userVOs);
		map.addAttribute("cid", cId);
		map.addAttribute("tid", tId);

		return "score";
	}

	@RequestMapping(value = "/addScore.do", method = RequestMethod.POST)
	public String addScore(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {

		String[] uids = request.getParameterValues("uid");
		String[] scores = request.getParameterValues("score");
		String cid = request.getParameter("cid");
		String tid = request.getParameter("tid");
		int c_id = Integer.parseInt(cid);
		List<ScoreModel> scoreModels = new ArrayList<ScoreModel>();
		for (int i = 0; i < uids.length; i++) {
			int uid = Integer.parseInt(uids[i]);
			ScoreModel model = scoreService.getScoreByUidAndCid(uid, c_id);
			model.setScore(Integer.parseInt(scores[i]));
			model.setCId(c_id);
			model.setTId(Integer.parseInt(tid));
			model.setUId(uid);
			scoreModels.add(model);
		}

		boolean isUpdate = scoreService.updateScore(scoreModels);
		if (isUpdate) {
			String url = "redirect:showuser.do?" + "cid=" + cid + "&tid=" + tid;
			return url;
		}
		return "error";
	}

	public void setScoreService(ScoreService scoreService) {
		this.scoreService = scoreService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
